﻿using System;
using System.Net;
using System.Net.Sockets;

using Common.Logging;

using SharpMail.Connections;


namespace SharpMail.MSA
{
    public class MessageSubmissionAgent
    {
        private IConnectionManager _connMgr;
        private TcpListener _listener;
        private ILog _logger;
        private MSAConfiguration _config;


        public bool IsRunning { get; private set; }
        public MSAConfiguration Configuration { get; set; }


        public MessageSubmissionAgent(ILog logger, IConnectionManager connMgr)
        {
            _logger = logger;
            _connMgr = connMgr;
        }


        public void Start()
        {
            _logger.Info("The message submission agent is starting.");

            _connMgr.OnConnection += new ConnectionHandler(_connMgr_OnConnection);

            //TODO: make this configurable
            _connMgr.AcceptConnectionsOn(new IPEndPoint(IPAddress.Any, 587));
            this.IsRunning = true;

            _logger.Info("The message submission agent has started.");
        }

        public void Stop()
        {
            _logger.Info("The message submission agent is stopping.");

            _connMgr.StopAcceptingConnections();
            this.IsRunning = false;

            _logger.Info("The message submission agent has stopped.");
        }


        public void DoMessageSubmission(MessageUserAgent mua)
        {
        }


        private void _connMgr_OnConnection(MessageUserAgent mua)
        {
            this.DoMessageSubmission(mua);
        }
    }
}
